// 路由监测
const fs = require('fs');
const sugar = loader('sugar')
module.exports=function(){
  // console.log('===============路由监控启动=================');
  //读取配置文件中的路由文件
  global.gem_config.routeConfig.forEach(({file,prefix="",midd})=>{
    // console.log("开始监控"+PATH+file);
    //监测所有的路由文件
    fs.watchFile(sugar.path(file),{
      interval:100,persistent:false
    },(cur, prv) => {
      global.clear();
      console.log('\033[30;43m CONDUCT \033[40;33m Compilation... '+file)
      console.time('\033[42;30m DONE \033[40;32m Compiled successfully in ');
      //运行一次路由
      loader("run_route",true).run_rout(file,prefix,midd);
      global.clear();
      console.timeEnd('\033[42;30m DONE \033[40;32m Compiled successfully in ');
      console.log();
    })
  })
  global.monitor[__filename]={stop};
}

//停止监测
function stop(){
  delete global.monitor[__filename];
  //关闭监测
  Object.keys(global.runRoute).forEach(res=>{
    fs.unwatchFile(sugar.path(res));
  })
  //清理当前文件缓存
  delete require.cache[__filename];
  //清空运行时路由
  global.runRoute={};
  return __filename;
}